package com.lmb.todo.controller;

import com.lmb.todo.pojo.Folder;
import com.lmb.todo.pojo.resp.Code;
import com.lmb.todo.pojo.resp.CommonResp;
import com.lmb.todo.service.FolderService;
import com.lmb.todo.utils.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/api/folder")
public class FolderController {

    @Autowired
    private FolderService folderService;

    @Autowired
    private AuthUtil authUtil;

    /**
    * 新建文件夹
    * */
    @PostMapping("")
    public CommonResp add(@RequestBody Folder folder){
        HashMap userInfo = authUtil.getAuthUserInfo();
        Integer userId = (Integer) userInfo.get("userId");
        folder.setUserId(userId);

        CommonResp resp = new CommonResp();
        Integer count = folderService.add(folder);

        if (count > 0) {
            resp.setCode(Code.HTTP_SUCCESS);
            resp.setMsg("OK!");
            return resp;
        }
        resp.setCode(Code.S_UNKNOWN_ERROR);
        resp.setMsg("Failed to add folder.");
        return resp;
    }

    /**
    * 批量删除文件夹
    * */
    @DeleteMapping("")
    public CommonResp delete(@RequestBody List<Integer> ids){
        CommonResp resp = new CommonResp();
        Integer count = folderService.deleteAll(ids);

        if (count > 0) {
            resp.setCode(Code.HTTP_SUCCESS);
            resp.setMsg("OK!");
            return resp;
        }
        resp.setCode(Code.C_DELETE_ERROR);
        resp.setMsg("delete failing.");
        return resp;
    }
}
